﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.Framework;
using System.Data;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin.CommonWindows
{
    public partial class ColumnSet : AdminPage
    {
        public string _ParentID = string.Empty;
        BLL.CNVP_Column bll = new BLL.CNVP_Column();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 23)) return;//判断页面权限
            Page.Title = "产品类别管理";
            _ParentID = Request.Params["ParentID"];
            if (!IsPostBack)
            {
                ProductTypeBind();
                if ((!string.IsNullOrEmpty(_ParentID)) && Public.IsNumber(_ParentID))
                {
                    DataInfo();
                }
                ButtonDelete.Attributes.Add("onclick", "if('" + _ParentID + "'==''){alert('请选择左边节点！');return false;}else{return confirm('确定删除该记录吗?');}");
            }
                
        }

        public void ProductTypeBind()
        {
            var ProductType_List = new BLL.CNVP_ProductType().GetAll_CNVP_ProductType();
            foreach (Model.CNVP_ProductType m in ProductType_List)
            {
                ddlProductTypeID.Items.Add(new ListItem(m.ProTypeName, m.ID.ToString()));
            }
        }
        private void DataInfo()
        {
            if (_ParentID == null)
            {
                BasePage.MessageBoxUp("请选择左边节点！");
                return;
            }
            Model.CNVP_Column item = new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(int.Parse(_ParentID));
            if (item != null)
            {
                this.txtID.Text = _ParentID;
                this.hfAddressRoute.Value = item.AddressRoute;
                this.txtDepth.Text = item.Depth.ToString();
                this.txtMenuName.Text = item.ColumnName;
                this.txtMenuUrl.Text = item.ColumnUrl;
                this.txtWeight.Text = item.OrderID.ToString();
                this.rblIsShow.SelectedValue = item.IsShow.ToString();
                this.rbAllowReview.SelectedValue = item.AllowReview.ToString();
                this.ddlProductTypeID.SelectedValue = item.ProductTypeID.ToString();
                this.lbSelectNodesText.Text = "选中的菜单项(节点):<br/><font color=\"blue\" size=\"5\">" + item.ColumnName + "</font>";
                this.lbSelectNodesText.Text += "<font size=\"4\">(" + _ParentID + ")</font>";
                //显示 当前选中节点的信息,可编辑
            }
            else
            {
                BasePage.MessageBoxUp("节点不存在，刷新左边！");
            }

        }

        protected void ButtonAddSubMenu_Click(object sender, EventArgs e)
        {
            foreach (Control c in this.Form.Controls)
            {
                if (c is TextBox)
                {
                    (c as TextBox).Text = "";
                }
            }
            if (_ParentID == null)
            {
                _ParentID = "0";
            }
            else
            {
                Model.CNVP_Column item = new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(int.Parse(_ParentID));
                this.hfAddressRoute.Value = item.AddressRoute + "," + _ParentID;
            }
            this.txtDepth.Text = _ParentID;
            this.txtWeight.Text = "0";
        }

        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            if (_ParentID == null)
            {
                BasePage.MessageBoxUp("请先选择左边节点！");
                return;
            }
            int CurrentNodeValue = int.Parse(_ParentID);

            var Products_List = new BLL.CNVP_Products().GetAll_CNVP_Products("ProductClassID=" + CurrentNodeValue + "");
            if (Products_List.Count > 0)
            {
                BasePage.MessageBoxUp("该分类存在产品列表中,不允许删除.！");
            }
            else
            {
                var Column_List = new BLL.CNVP_Column().GetAll_CNVP_Column(" Depth =" + CurrentNodeValue + "");
                if (Column_List.Count > 0)
                {
                    BasePage.MessageBoxUp("该节点存在子节点,不允许删除.！");
                }
                else
                {
                    int rows = new BLL.CNVP_Column().Delete_CNVP_Column(CurrentNodeValue);
                    if (rows == 1)
                    {
                        BasePage.MessageBoxUp("删除成功！");
                        this.lbSelectNodesText.Text = "";
                        this.txtID.Text = "";
                        this.txtDepth.Text = "0";
                        this.txtMenuName.Text = "";
                        this.txtMenuUrl.Text = "";
                        this.txtWeight.Text = "0";
                        this.rblIsShow.SelectedValue = "0";
                        this.rbAllowReview.SelectedValue = "0";
                        this.hfAddressRoute.Value = "0";
                    }
                }
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            if (txtID.Text.Trim() == "")
            {
                Model.CNVP_Column model = new Model.CNVP_Column();
                model.Depth = txtDepth.Text.Trim();
                model.ColumnName = txtMenuName.Text.Trim();
                model.ColumnType = 3;//‘3’代表商品分类
                model.AddressRoute = (this.hfAddressRoute.Value == "" || this.hfAddressRoute.Value == "0") ? "0" : this.hfAddressRoute.Value;
                model.ColumnUrl = txtMenuUrl.Text.Trim();
                model.TemplateUrl = ddlTemplateUrl.SelectedValue;
                model.OrderID = int.Parse(txtWeight.Text.Trim());
                model.ProductTypeID = int.Parse(ddlProductTypeID.SelectedValue);
                model.IsShow = byte.Parse(rblIsShow.SelectedValue);
                model.AllowReview = byte.Parse( rbAllowReview.SelectedValue);
                bll.Add_CNVP_Column(model);
                BasePage.MessageBoxUp("添加成功！");
            }
            else
            {
                Model.CNVP_Column model = new Model.CNVP_Column();
                model.ID = int.Parse(this.txtID.Text);
                model.Depth = txtDepth.Text.Trim();
                model.ColumnName = txtMenuName.Text.Trim();
                model.ColumnType = 3;//‘3’代表商品分类
                model.AddressRoute = this.hfAddressRoute.Value == "" ? "0" : this.hfAddressRoute.Value;
                model.ColumnUrl = txtMenuUrl.Text.Trim();
                model.TemplateUrl = ddlTemplateUrl.SelectedValue;
                model.OrderID = int.Parse(txtWeight.Text.Trim());
                model.ProductTypeID = int.Parse(ddlProductTypeID.SelectedValue);
                model.IsShow = byte.Parse(rblIsShow.SelectedValue);
                model.AllowReview = byte.Parse( rbAllowReview.SelectedValue);
                bll.Update_CNVP_Column(model);
                BasePage.MessageBoxUp("修改成功！");
            }
        }
    }
}